/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.plugins');
(function () {
	var EVENTS = net.autodata.EventBus.EVENTS;
	
	
	var Plugin = Ext.extend(Ext.util.Observable, {
	
		panel: undefined,
		colourPicker: undefined,
		
		init: function (panel)
		{
			this.panel = panel;
			var view = panel.getView();
			
			var session = net.autodata.Session.getInstance();
			var bv = session.vehicles.getBaseVehicle();
			
			this.colourPicker = new net.autodata.compare.colourpicker.ColourPicker({		
				swatchData: bv ? bv.getSelectedSwatch() : undefined,
				storeCfg: {
					proxy: new net.autodata.compare.data.ColourPickerSessionProxy()
				},
				
				listeners: {
					scope: this,
					'colourselected': this.onColourSelected
				}
			});
		
			panel.on('destroy', this.onDestroy, this);
			view.on('refresh', this.onViewRefresh, this);
			
			var eb = net.autodata.EventBus.getInstance();			
			eb.on(EVENTS.READY, this.onEventBusReady, this);
			
		},
		
		onEventBusReady: function ()
		{
			this.colourPicker.getStore().load();
		},
		
		onViewRefresh: function ()
		{
			var view = this.panel.getView();
			this.colourPicker.render(view.baseVehiclePanel.el.dom);
		},
		
		onColourSelected: function (swatch)
		{
			var eb = net.autodata.EventBus.getInstance();
			eb.fireEvent(EVENTS.CHANGE_BASE_IMAGE, swatch);
			
			var session = net.autodata.Session.getInstance();
			var acodes = [];
			session.vehicles.eachCompetitor(function (competitor) {
				acodes.push(competitor ? competitor.acode : '');
			}, this);
			
			var cds = net.autodata.compare.data.CompetitorDataStore.getInstance();
			cds.loadImages(acodes, swatch.shortDesc, swatch.colourCode);
		},
		
		onDestroy: function ()
		{
			var p = this.panel;
			var v = p.getView();
			p.un('destroy', this.onDestroy, this);
			v.un('refresh', this.onAfterRender, this);
			
			var eb = net.autodata.EventBus.getInstance();			
			eb.un(EVENTS.READY, this.onEventBusReady, this);
		}
		
	});
	
	net.autodata.compare.plugins.CompareVehiclesColourPicker = Plugin;
	
})();